#!/usr/bin  python
# _*_ coding:utf-8 _*_
"""
----------------------------------
    File Name: baby
    Description:
        Author: Dell
        Date: 2020/8/6
-----------------------------------
"""
from rest_framework import serializers
from Baby import models
from Common import bcrypt


class BabyModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Babies
        fields = ("id", "hospital", "EMR_Name", "App_Name", "birthday", "expected_birthday", "gender", "MRN", "group",
                  "status", "discharged", "first_name", "last_name", "mrn_hash", "validated", "created_time")
        extra_kwargs = {
            "id": {
                "read_only": True
            },
            "EMR_Name": {
                "read_only": True
            },
            "App_Name": {
                "read_only": True
            },
            "MRN": {
                "read_only": True
            },
            "validated": {
                "read_only": True
            },
            "created_time": {
                "read_only": True
            },
            "mrn_hash": {
                "write_only": True
            }
        }

    def validate(self, attrs):
        attrs["full_name"] = "{} {}".format(attrs["first_name"], attrs["last_name"])
        attrs["mrn_last4"] = attrs["mrn_hash"][-4:]
        attrs["mrn_hash"] = bcrypt.Bcrypt.encrypt(attrs["mrn_hash"])
        return attrs
